package com.gw.vip.server.service;

import com.gw.vip.common.exception.ExceptionEnums;
import com.gw.vip.common.exception.ServiceException;
import com.gw.vip.server.bo.IssuInfo;
import com.gw.vip.server.bo.MailSenderInfo;
import com.gw.vip.server.constants.SysArgKey;
import com.gw.vip.server.dto.SetIssuInfoDto;
import com.gw.vip.server.platform.vipcenter.VipCenterService;
import com.gw.vip.server.platform.vipcenter.dto.VipCenterIssuInfoDto;
import com.gw.vip.server.platform.vipcenter.dto.VipCenterSetMailSenderInfoDto;
import com.gw.vip.server.platform.vipcenter.vo.VipCenterIssuInfoVo;
import com.gw.vip.server.service.dao_service.SysArgService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author GW
 * @description
 * @since 2022-09-23
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SettingService {

    private final SysArgService sysArgService;
    private final VipCenterService vipCenterService;

    /**
     * 设置发行机构信息
     * @param dto
     */
    public void setIssuInfo(SetIssuInfoDto dto) {
        IssuInfo issuInfo = sysArgService.getObject(SysArgKey.ISSU_INFO, IssuInfo.class);
        if (issuInfo!=null){
            log.error("已经存在发行机构，不可重复设置");
            throw new ServiceException(ExceptionEnums.BUSINESS_FORBIDDEN, "已经存在发行机构，不可重复设置");
        }

        //向中心获取发行机构信息
        VipCenterIssuInfoDto vipCenterIssuInfoDto = new VipCenterIssuInfoDto();
        vipCenterIssuInfoDto.setMailAddress(dto.getMailAddress());
        vipCenterIssuInfoDto.setPassword(dto.getPassword());
        VipCenterIssuInfoVo vipCenterIssuInfoVo = vipCenterService.issuInfo(vipCenterIssuInfoDto);

        issuInfo = new IssuInfo();
        issuInfo.setId(vipCenterIssuInfoVo.getId());
        issuInfo.setUserId(vipCenterIssuInfoVo.getUserId());
        issuInfo.setName(vipCenterIssuInfoVo.getName());
        issuInfo.setAccessKey(vipCenterIssuInfoVo.getAccessKey());
        issuInfo.setStatus(vipCenterIssuInfoVo.getStatus());
        sysArgService.setObject(SysArgKey.ISSU_INFO, "发行机构信息", issuInfo, null);
    }

    /**
     * 设置邮件发送者信息
     * @param userName
     * @param password
     */
    @Transactional(rollbackFor = Exception.class)
    public void setMailInfo(String userName, String password) {
        IssuInfo issuInfo = sysArgService.getObject(SysArgKey.ISSU_INFO, IssuInfo.class);
        if (issuInfo==null){
            log.error("发行机构不存在！");
            throw new ServiceException("发行机构不存在！请先设置发行机构");
        }

        MailSenderInfo mailSenderInfo = new MailSenderInfo();
        mailSenderInfo.setUserName(userName);
        mailSenderInfo.setPassword(password);
        sysArgService.setObject(SysArgKey.MAIL_SENDER_INFO, "邮件发送者信息", mailSenderInfo, null);

        VipCenterSetMailSenderInfoDto dto = new VipCenterSetMailSenderInfoDto();
        dto.setUserId(issuInfo.getUserId());
        dto.setUserName(userName);
        dto.setPassword(password);
        vipCenterService.setMailSenderInfo(dto);
    }
}
